package com.bdzl.credit.controller;

import com.bdzl.credit.common.enums.BrEnum;
import com.bdzl.credit.service.CreditReportService;
import com.bdzl.credit.service.CreditService;
import com.bdzl.credit.servicefeign.FeignAdminService;
import com.bdzl.pub.entity.loan.CreditDO;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.loan.CreditVO;
import net.sf.json.JSONObject;
import com.bdzl.pub.entity.sys.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/bdzl/creditReport")
public class CreditReportController {
    @Autowired
    private CreditReportService creditReportService;
    @Autowired
    private CreditService creditService;
    @Autowired
    private FeignAdminService feignAdminService;

    /**
     * @Author Zhn
     * @Description //TODO 征信查询接口
     * @Date 10:35 2021/5/18
     * @Param [name, call, id] 名称  电话  身份证
     * @return java.util.Map   "see" 查看地址   "save"下载地址
     **/
    @PostMapping("/getCredit")
    @ResponseBody
    @Transactional
    public R getCredit(@RequestBody UserDO userDO){
        //策略代码//策略代码
        String webPwd = feignAdminService.getByConfigNo(BrEnum.策略编号贷前征信.getValue()).getConfigVal();
        //策略引擎接口  生成报告  并返流水号
        JSONObject main = creditReportService.CreditReport(userDO.getUserNickname(),userDO.getUserPhone(),userDO.getUserCardid(), BrEnum.策略编号贷前征信.getValue(),webPwd);
        //取出流水号
        String swift_number = main.get("swift_number").toString();
        //下载pdf并上传
        String brPdfUrl = creditReportService.getBrPdfUrl(userDO.getUserCardid(),swift_number);
        if (brPdfUrl.equals("100")){
            return R.error("客户端嵌入式报告的token获取失败！");
        } else if (brPdfUrl.equals("500")) {
            return R.error("阿里云上传失败！");
        }
        //生成征信记录
        CreditDO creditDO = new CreditDO();
        creditDO.setCreditApiType("百融");
        creditDO.setCreditCardid(userDO.getUserCardid());
        creditDO.setCreditFilePath(brPdfUrl);
        creditDO.setCreditInfo(main.toString());
        creditDO.setCreditCreuser(userDO.getUserId());
        creditService.save(creditDO);
        CreditVO creditByCardId = creditService.getCreditByCardId(userDO.getUserCardid());
        return R.ok(creditByCardId);
    }

    /**
     * @Author Zhn
     * @Description //TODO 征信查询接口
     * @Date 10:35 2021/5/18
     * @Param [name, call, id] 名称  电话  身份证
     * @return java.util.Map   "see" 查看地址   "save"下载地址
     **/
    @PostMapping("/getCreditAgain")
    @ResponseBody
    @Transactional
    public R getCreditAgain(@RequestBody UserDO userDO){
        //策略代码//策略代码
        String webPwd = feignAdminService.getByConfigNo(BrEnum.策略编号贷前征信.getValue()).getConfigVal();
        //策略引擎接口  生成报告  并返流水号
        JSONObject main = creditReportService.CreditReport(userDO.getUserNickname(),userDO.getUserPhone(),userDO.getUserCardid(), BrEnum.策略编号贷前征信.getValue(),webPwd);
        //取出流水号
        String swift_number = main.get("swift_number").toString();
        //下载pdf并上传
        String brPdfUrl = creditReportService.getBrPdfUrl(userDO.getUserCardid(),swift_number);

        if (brPdfUrl.equals("100")){
            return R.error("客户端嵌入式报告的token获取失败！");
        } else if (brPdfUrl.equals("500")) {
            return R.error("阿里云上传失败！");
        }
        //生成征信记录
        CreditDO creditByCardId = creditService.getCreditByCardId(userDO.getUserCardid());
        //获取旧头像
        if (creditByCardId.getCreditFilePath()!=null && creditByCardId.getCreditFilePath().startsWith("http")){
            OssUploadFileUtil.remove(creditByCardId.getCreditFilePath());
        }
        creditByCardId.setCreditFilePath(brPdfUrl);
        creditByCardId.setCreditInfo(main.toString());
        creditByCardId.setCreditModuser(userDO.getUserId());
        creditService.update(creditByCardId);
        return R.ok(creditByCardId);
    }


}